# FreeBench tool building library
# (c) 2008 Thomas Stromberg <thomas%stromberg.org>

import sys
import datetime
import os

# This needs to be made tunable again.
VERBOSE = os.getenv('VERBOSE', False)

def debug(string):
  if VERBOSE:
    print ': %s' % string

def msg(string):
  print "- %s" % string
  sys.stdout.flush()

def avg(sequence):
 return float(sum(sequence)) / float(len(sequence))

def chdir(dir):
  debug("-> %s" % dir)
  os.chdir(dir)
 
def mkdir_p(dir):
  try:
    debug('mkdir %s' % dir)
    os.makedirs(dir)
  except OSError:
    pass
  
def delta_seconds(delta):
  if isinstance(delta, datetime.timedelta):
    return float(delta.days * 86400) + float(delta.seconds) + (float(delta.microseconds) / 1000000)
  else:
    return None
  
def set_library_path(path):
  ostype = os.uname()[0]
  if ostype == 'AIX':
    var = 'LIBPATH'
  elif ostype == 'HP-UX':
    var = 'SHLIB_PATH'
  elif ostype == 'Darwin':
    var = 'DYLD_LIBRARY_PATH'
  else:
    var = 'LD_LIBRARY_PATH'
  debug('Set %s=%s' % (var, path))
  os.putenv(var, path)

  
